package cn.jiyun.user.service.impl;

import cn.jiyun.user.mapper.UserMapper;
import cn.jiyun.user.pojo.User;
import cn.jiyun.user.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
   @Autowired
   private RedisTemplate redisTemplate;
    @Override
    public String findAll() throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        //查询所有
        String userListStr = (String)redisTemplate.boundValueOps("user_list").get();
        if (StringUtils.isEmpty(userListStr)){
            //查询数据库
            List<User> userList = userMapper.selectList(null);
            userListStr =objectMapper.writeValueAsString(userList);
            //存入Redis
            redisTemplate.boundValueOps("user_list").get();
            System.out.println("查询数据库。。。");
        }else {
            System.out.println("查询redis。。。");
        }
        return userListStr;
    }
}
